package com.shiguiwu.springmybatis.mybatis;

import com.shiguiwu.springmybatis.mybatis.mapper.BookMapper;
import org.apache.ibatis.session.SqlSession;

/**
 * @description: 测试
 * @author: stone
 * @date: Created by 2021/7/1 15:50
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.mybatis
 */
public class CacheMybatisTests {

    //第一种方式，插入数据会清空
    public static void main1(String[] args) {
        SqlSession sqlSession = MapperTests.FACTORY.openSession(true);
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        bookMapper.getBookList();
        Book book = new Book();
        book.setBookName("草房子");
        //一级缓存清除
        bookMapper.insert(book);

        bookMapper.getBookList();
    }

    //第二种方式，SqlSession提供的api
    public static void main2(String[] args) {
        SqlSession sqlSession = MapperTests.FACTORY.openSession(true);
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        bookMapper.getBookList();

        //一级缓存清除
        sqlSession.clearCache();

        bookMapper.getBookList();
    }

    //flushCache=true 在xml中配置
    public static void main(String[] args) {
        SqlSession sqlSession = MapperTests.FACTORY.openSession(true);
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        bookMapper.getBookList();

        bookMapper.getBookList();
    }

}
